/**
 * @file
 * @author  Tsin Smith <tsin.creator@gmail.com>
 *
 * @section DESCRIPTION
 *
 */

#ifndef __TSIN_LIBRARY_DYNAMICINIT_HPP__
#define __TSIN_LIBRARY_DYNAMICINIT_HPP__

#include <tsin/library/common.hpp>
#include <tsin/util/RefCnt.hpp>

#include <scdl/parser/Symbol.hpp>

namespace tsin {

        typedef unsigned long DynamicEvent;

        const DynamicEvent EVENT_NONE = 0;
        const DynamicEvent EVENT_TRIGGER = 0x1;
        const DynamicEvent EVENT_END = 0x2;
        const DynamicEvent EVENT_REINIT = 0x4;

        class LIBRARY_DLLEXPORT DynamicObject : public RefCnt {
        public:
                static bool openLibrary(const char *path);
                static DynamicObject *interpret(scdl::Symbol *sym);

                DynamicObject(bool d = false);
                virtual ~DynamicObject();

                void appendArgument(DynamicObject *arg);
                bool isDynamic();
                virtual DynamicObject *copy();
                virtual void reinit() = 0;
                virtual DynamicEvent preTick(void *data = 0, void *priv_data = 0);
                virtual DynamicEvent postTick(void *data = 0, void *priv_data = 0);

                virtual char getDescriptor() = 0;
                virtual int getInt();
                virtual float getFloat();
                virtual float getFloatInRad();
                virtual bool getBool();
                virtual const char *getString();

        protected:
                DynamicObject *copy(DynamicObject *obj);

                float getData(void *data, int idx);

                int nrArguments;
                DynamicObject **arguments;
                char *desp;

        private:
                static void *libHandle;
                static DynamicObject *interpretInterface(scdl::Identifier *sym);
                static DynamicObject *interpretApplication(scdl::Application *sym);
                static DynamicObject *interpretList(scdl::List *sym);

                bool dynamic;
        };
}

typedef tsin::DynamicObject *(*Binder)();

#endif /* __TSIN_LIBRARY_DYNAMICINIT_HPP__ */
